package com.ad.payload.util;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Map;

public class ObjectUtil {

    /**
     * map 转 对象
     * @param map
     * @param vo
     */
    public static void mapToObj(Map<String,Object> map, Object vo) {
        try {
            if (map==null || vo==null) {
                System.out.println("VOUtil.copyVO 参数[map] or [vo] is null");
                return;
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Method[] methodList = vo.getClass().getDeclaredMethods();
            String key = "", vType="", mName="", mmName="", mType="", mValue;
            Object value = null;

            for (Method m : methodList) {
                mName = m.getName();
                if (mName.startsWith("set")) {
                    mName = m.getName().substring(3);
                    mmName = mName.substring(0,1).toLowerCase();
                    key = mmName + m.getName().substring(4);

                    if (!map.containsKey(key))
                        continue;

                    value = map.get(key);
                    if (value == null)
                        continue;

                    //vType = value.getClass().getCanonicalName();
                    //mType = m.getParameterTypes()[0].getCanonicalName();
                    m.invoke(vo, value);

                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}
